//传入从机地址和PLC组态,返回查询指令
const crc = require('./crc16')

function getEnquire(slaverIndex,plc,config={}){
    config.len = config.len?config.len:'1';
    config.buff = config.buff?config.buff:false
    const {len,buff} = config;
    let funCode,address = '';
    if(plc[0] === '4'){
        funCode = '03'
    }
    else if(plc[0] === '3'){
        funCode = '04'
    }
    else if(plc[0] === '1'){
        funCode = '02'
    }
    else if(plc[0] === '0'){
        funCode = '01'
    }
    else{return('0')}
    const norAdress = (plc.substr(1) -1).toString(16)
    for(let i = norAdress.length;i<4;i++){
        address =  address + '0'
    }


    if(slaverIndex.length == 1){
        slaverIndex = '0' + slaverIndex
    }

    let output = slaverIndex + funCode + address +norAdress+ '000' +len
    output = output + crc(output)
    if(buff){
        return Buffer.from(output,'hex')
    }else{
        return output
    }
    
    
}


module.exports = getEnquire;